<template>
  <div class="settings-container" :style="{ '--local-mount-height': cssVar ? `var(${cssVar})` : '100%' }">
    <el-tabs tab-position="left" class="settings-tabs" :style="{ '--local-mount-height': cssVar ? `var(${cssVar})` : '100%' }"  v-model="activeTab">
      <el-tab-pane :label="$t('message.setting.core')" name="base">
        <el-scrollbar class="settings-content" :style="{ '--local-mount-height': cssVar ? `var(${cssVar})` : '100%' }" >
          <div>
            <el-card class="settings-card">
              <div class="card-header">
                <el-icon class="icon">
                  <Setting />
                </el-icon>
                <h3>{{ $t('message.setting.core') }}</h3>
                <User/>
              </div>

              <el-form label-position="top" class="settings-form">
                <el-row class="mt5" :gutter="20">
                  <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="6">
                    <el-form-item :label="$t('message.setting.sdEngine')" class="form-item">
                      <el-select v-model="configStore.config.sdEngine" :placeholder="$t('message.setting.sdEngine')"
                        class="select-input" @change="handleEngineChange">
                        <el-option label="sd-webui" value="webui" />
                        <el-option label="comfyui" value="comfyui" />
                      </el-select>
                    </el-form-item>
                  </el-col>

                  <el-col v-show="configStore.config.sdEngine == 'webui'" :xs="24" :sm="12" :md="12" :lg="8" :xl="6">
                    <el-form-item :label="$t('message.setting.webuiUrl')" class="form-item">
                      <el-input v-model="configStore.config.sdwebuiPath" :placeholder="$t('message.setting.webuiUrl')"
                        class="text-input" />
                    </el-form-item>
                  </el-col>

                  <el-col v-show="configStore.config.sdEngine == 'comfyui'" :xs="24" :sm="12" :md="12" :lg="8" :xl="6">
                    <el-form-item :label="$t('message.setting.comfyuiUrl')" class="form-item">
                      <el-input v-model="configStore.config.comfyuiPath" :placeholder="$t('message.setting.comfyuiUrl')"
                        class="text-input" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row class="mt10" :gutter="20">
                  <el-col :span="24">
                    <el-form-item :label="$t('message.setting.baiduUrl')" class="form-item">
                      <el-input v-model="configStore.config.baiduUrl" :placeholder="$t('message.setting.baiduUrl')"
                        class="text-input" />
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row class="mt10" :gutter="20">
                  <el-col :span="24">
                    <el-form-item :label="$t('message.setting.appid')" class="form-item">
                      <el-input v-model="configStore.config.appid" :placeholder="$t('message.setting.appid')"
                        class="text-input" />
                    </el-form-item>
                  </el-col>
                </el-row>

                <el-row class="mt10" :gutter="20">
                  <el-col :span="24">
                    <el-form-item :label="$t('message.setting.appkey')" class="form-item">
                      <el-input v-model="configStore.config.appkey" :placeholder="$t('message.setting.appkey')"
                        class="text-input" />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row class="mt10" :gutter="20">
                  <el-col :span="16">
                    <el-form-item :label="$t('message.setting.imagesPath')" class="form-item">
                      <el-input v-model="configStore.config.imagesPath"
                        :placeholder="$t('message.setting.imagesPath')" class="text-input" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-button icon="FolderOpened" type="primary" class="select-btn"
                      @click="configStore.handleSelectImagesPath">
                      {{ $t('message.setting.imagesPath') }}
                    </el-button>
                  </el-col>
                </el-row>
              </el-form>

              <div class="card-footer">
                <el-button icon="ele-Refresh" type="primary" class="save-btn" @click="configStore.saveConfig({})">
                  {{ $t('message.common.save') }}
                </el-button>
              </div>
            </el-card>
          </div>
        </el-scrollbar>
      </el-tab-pane>
      <!--
      <el-tab-pane label="高级配置" name="config">
        <el-scrollbar class="settings-content">
          <div>
            <el-card class="settings-card">
              <div class="card-header">
                <el-icon class="icon">
                  <Cpu />
                </el-icon>
                <h3>系统设置</h3>
              </div>

              <div class="config-grid">
                <div class="config-item">
                  <div class="config-icon">
                    <el-icon :size="60">
                      <DataAnalysis />
                    </el-icon>
                  </div>
                  <div class="config-info">
                    <h4>性能配置</h4>
                    <p>调整系统资源分配和性能参数</p>
                  </div>
                </div>

                <div class="config-item">
                  <div class="config-icon">
                    <el-icon :size="60">
                      <Key />
                    </el-icon>
                  </div>
                  <div class="config-info">
                    <h4>API密钥</h4>
                    <p>管理第三方服务的访问密钥</p>
                  </div>
                </div>

                <div class="config-item">
                  <div class="config-icon">
                    <el-icon :size="60">
                      <Cloudy />
                    </el-icon>
                  </div>
                  <div class="config-info">
                    <h4>云存储</h4>
                    <p>配置云存储服务和同步设置</p>
                  </div>
                </div>
              </div>
            </el-card>

            <el-card class="settings-card" style="margin-top: 20px;">
              <div class="card-header">
                <el-icon class="icon">
                  <Document />
                </el-icon>
                <h3>自定义配置</h3>
              </div>

              <el-input v-model="text" type="textarea" :rows="6" placeholder="输入自定义配置 (JSON格式)"
                class="custom-config-input" />
            </el-card>
          </div>
        </el-scrollbar>
      </el-tab-pane>-->
    </el-tabs>
  </div>
</template>

<script setup lang="ts" name="config">
import { useConfigStore } from '@renderer/stores/configStore';
import { defineAsyncComponent, getCurrentInstance, ref } from 'vue';
import { useModelStore } from '@renderer/stores/useModelStore';

const instance = getCurrentInstance();
const cssVar = instance?.appContext.config.globalProperties.$cssVarName;
const configStore = useConfigStore();
const activeTab = ref('base');
const modelStore = useModelStore()
const User = defineAsyncComponent(() => import('@renderer/layout/navBars/topBar/user.vue'))

const handleEngineChange = (value: string) => {
  modelStore.fetchModels();
}
</script>

<style scoped lang="scss">
.settings-container {
  height: calc(var(--local-mount-height) - 125px);
  padding: 20px;
}

.settings-tabs {
  height: calc(var(--local-mount-height) - 125px);

  :deep(.el-tabs__header) {
    width: 200px;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    margin-right: 20px;
    padding: 15px 0;
    background: var(--el-bg-color);

    .el-tabs__nav {
      width: 100%;
    }

    .el-tabs__item {
      height: 60px;
      padding: 0 24px !important;
      display: flex;
      align-items: center;
      color: var(--el-text-color-regular);
      font-size: 16px;
      font-weight: 500;
      transition: all 0.3s ease;
      border-left: 4px solid transparent;

      &:hover {
        color: var(--el-color-primary);
        background: var(--el-color-primary-light-9);
      }

      &.is-active {
        color: var(--el-color-primary);
        background: var(--el-color-primary-light-9);
        border-left: 4px solid var(--el-color-primary);
        font-weight: 600;
      }
    }
  }

  :deep(.el-tabs__content) {
    flex: 1;
    background: transparent;
    height: 100%;
    overflow-y: auto;
    padding-right: 10px;
  }
}

.settings-content {
  height: calc(var(--local-mount-height) - 125px);
  max-width: 1200px;
  margin: 0 auto;
}

.settings-card {
  border-radius: 16px;
  border: none;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.05),
    0 10px 10px -5px rgba(0, 0, 0, 0.02);
  margin-bottom: 20px;
  overflow: visible;
  background: var(--el-bg-color-overlay);

  .card-header {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    margin-bottom: 20px;
    border-bottom: 1px solid var(--el-border-color-light);

    .icon {
      background: var(--el-color-primary-light-9);
      color: var(--el-color-primary);
      border-radius: 12px;
      padding: 12px;
      margin-right: 15px;
    }

    h3 {
      max-width: 100px;
      font-size: 1.4rem;
      font-weight: 600;
      color: var(--el-text-color-primary);
      margin: 0;
    }
  }
}

.settings-form {
  padding: 0 15px;

  .form-item {
    margin-bottom: 25px;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: var(--el-text-color-regular);
      margin-bottom: 8px;
      font-size: 0.95rem;
    }
  }

  .select-input {
    width: 100%;
    border-radius: 10px;

    :deep(.el-input__inner) {
      border-radius: 10px;
      height: 45px;
      padding-left: 15px;
      background: var(--el-fill-color-lighter);
      border: 1px solid var(--el-border-color);
      transition: all 0.3s ease;

      &:hover {
        border-color: var(--el-border-color-hover);
      }

      &:focus {
        border-color: var(--el-border-color);
        box-shadow: 0 0 0 3px var(--el-color-primary-light-7);
      }
    }
  }
}

.select-btn {
  width: 100%;
  height: 45px;
  border-radius: 10px;
  background: var(--el-color-primary);
  border: none;
  font-weight: 500;
  transition: all 0.3s ease;
  margin-top: 30px;
  color: var(--el-color-white);

  &:hover {
    background: var(--el-color-primary-light-3);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px var(--el-color-primary-light-5);
  }
}

.save-btn {
  padding: 12px 30px;
  border-radius: 10px;
  font-weight: 500;
  background: linear-gradient(135deg, var(--el-color-primary), var(--el-color-primary-dark-2));
  border: none;
  transition: all 0.3s ease;
  color: var(--el-color-white);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 12px var(--el-color-primary-light-5);
  }
}

.card-footer {
  display: flex;
  justify-content: flex-end;
  padding: 20px 15px 10px;
  border-top: 1px solid var(--el-border-color-lighter);
  margin-top: 10px;
}

.config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  padding: 0 15px;
}

.config-item {
  display: flex;
  background: var(--el-fill-color-lighter);
  border-radius: 12px;
  padding: 20px;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid var(--el-border-color-lighter);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
    border-color: var(--el-border-color);
  }

  .config-icon {
    margin-right: 15px;
    color: var(--el-color-primary);
  }

  .config-info {
    h4 {
      margin: 0 0 8px 0;
      font-size: 1.1rem;
      color: var(--el-text-color-primary);
    }

    p {
      margin: 0;
      color: var(--el-text-color-secondary);
      font-size: 0.9rem;
    }
  }
}

.custom-config-input {
  margin: 0 15px 15px;
  width: calc(100% - 30px);

  :deep(.el-textarea__inner) {
    border-radius: 12px;
    padding: 15px;
    border: 1px solid var(--el-border-color);
    background: var(--el-fill-color-lighter);
    font-family: monospace;
    font-size: 14px;
    transition: all 0.3s ease;

    &:hover {
      border-color: var(--el-border-color-hover);
    }

    &:focus {
      border-color: var(--el-color-primary);
      box-shadow: 0 0 0 3px var(--el-color-primary-light-7);
    }
  }
}

@media (max-width: 768px) {
  .settings-tabs {
    :deep(.el-tabs__header) {
      width: 100%;
      margin-right: 0;
      margin-bottom: 20px;
    }

    :deep(.el-tabs__content) {
      padding-right: 0;
    }
  }

  .config-grid {
    grid-template-columns: 1fr;
  }
}
</style>
